<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\app\service;
use base\files\Poster;
use think\facade\Session;
use think\Db;
use base\service\HttpService;
use base\date\Dates;
use base\other\String;
use auth\Api;

class User 
{

	/**
	* 错误信息
	*/
	public  $errorInfo;
	
	/**
	* appkey
	*/
	private  $appkey='FODGYEVS';
	/**
	* api
	*/
	private  $apiInfo = [
		'register'=>'http://115.236.50.19:23095/openapi_dev/ztt/cloud/service/driver/register',
		'query'=>'http://115.236.50.19:23095/openapi_dev/ztt/cloud/service/driver/query'
	];
	
	
	/**
	* 请求
	*/
	private function request($url,array $param=array()){
		
		$param['appkey'] = $this->appkey;
		$param['nonce'] = Strings::getRandomStr(32);
		$param['time'] = Dates::getMillisecond();
		
		//生成token 并请求第三方
		$token = Api::instance()->getToken($param);
		$result = HttpService::postRequest($url.'?token='.$token,json_encode($param),true);
		$result = json_decode($result,true);
		
		if(!$result){
			$this->errorInfo = "请求第三方接口错误！";
			return false;
		}
		
		if($result['errorCode'] != 0){
			$this->errorInfo = $result['errorMessage'];
			return false;
		}
		
		return $result['data'];
	}
	
	
	/**
	* 注册
	*/
	public function register($param){
		
		$data['username']  = $param['username'];
		$data['password']  = $param['password'];
		$data['nickname']  = preg_replace_callback('/./u',function (array $match) {
		    return strlen($match[0]) >= 4 ? '' : $match[0];
		},$param['nickname']);
		$data['phone']  = $param['phone'];
		return   $this->request($this->apiInfo['register'],$data);
		
	}
	
	/**
	* 查询用户信息
	*/
	public function queryUserInfo($param){
		
		$data['username']  = $param['username'];
		$data['phone']  = $param['phone'];
		return   $this->request($this->apiInfo['query'],$data);
		
	}
}

